/**
 * MIT License
 * 
 * Copyright (c) 2024 - present @ ebraid
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

#include <stdint.h>
#include <kernel/initcall.h>
#include <kernel/softirq.h>
#include <kernel/syslog.h>
#include <list.h>
#include <os/mutex.h>
#include <os/task.h>
#include <config.h>


#define SOFTIRQD_PRIORITY  (CONFIG_OS_PRIORITY_MAX - 2)


static struct mutex softirq_lock = {NULL};
static int current = -1;
static LIST_HEAD(tasklet_list);


static struct softirq_vet {
    unsigned long state;
    void (*action[NR_SOFTIRQS])(void);
} softirq_vet;


static inline void set_pending(int index)
{
    softirq_vet.state |= (1 << index);
}


static inline void clear_pending(int index)
{
    softirq_vet.state &= ~(1 << index);
}


/**
 * @brief register a softirq handler
 * @param index softirq index
 * @param action softirq handler
 * @return none
 */
void softirq_register(int index, void (*action)(void))
{
    softirq_vet.action[index] = action;
}


/**
 * @brief wakeup softirq
 * @param index softirq index
 * @return none
 */
void softirq_wakeup(int index)
{
    current = index;
    set_pending(index);
    mutex_unlock(&softirq_lock);
}


static void softirqd_kthread(void *param)
{
    /* first, get the softirq lock, ensure that the softirq is pending */
    mutex_lock(&softirq_lock);

    while(1) {
        /* wait for softirq trigger */
        mutex_lock(&softirq_lock);

        DEBUG_TRACE("softirqd wakeup softirq %d", current);

        if((softirq_vet.state & (1 << current)) && softirq_vet.action[current]) {
            softirq_vet.action[current]();
            clear_pending(current);
        }
    }
}


static void tasklet_action_common(void)
{
    struct tasklet *tl;

    list_for_each_entry(tl, &tasklet_list, struct tasklet, entry) {
        if(tl->state) {
            tl->func(tl->data);
            tl->state = 0;
        }
    }
}


static int softirq_init(void)
{
    for(int i = 0; i < NR_SOFTIRQS; i++) {
        softirq_vet.action[i] = NULL;
        softirq_vet.state = 0;
    }

    mutex_init(&softirq_lock);

    /* tasklet action */
    softirq_vet.action[TASKLET_SOFTIRQ] = tasklet_action_common;

    /* create softirqd daemon thread */
    task_create(softirqd_kthread, "softirqd", 256, NULL, 5);
    return 0;
}


core_initcall(softirq_init);


/**
 * @brief add a tasklet to the tasklet list
 * @param tasklet tasklet
 * @param func tasklet handler
 * @param data tasklet data
 * @return none
 */
void tasklet_init(struct tasklet *tasklet, void (*func)(unsigned long data), unsigned long data)
{
    tasklet->func = func;
    tasklet->data = data;
    tasklet->state = 0;
}


/**
 * @brief add a tasklet to the tasklet list, then wakeup the tasklet
 * @param tasklet tasklet
 * @return none
 */
void tasklet_schedule(struct tasklet *tasklet)
{
    if(!tasklet->state) {
        tasklet->state = 1;
        list_add_node_at_tail(&tasklet_list, &tasklet->entry);
        softirq_wakeup(TASKLET_SOFTIRQ);
    }
}


/**
 * @brief trigger tasklet action
 * @param tasklet tasklet
 * @param data parameter data
 * @return none
 */
void tasklet_action(struct tasklet *tasklet, unsigned long data)
{
    if(!tasklet->state) {
        tasklet->data = data;
        tasklet->state = 1;
        softirq_wakeup(TASKLET_SOFTIRQ);
    }
}


/**
 * @brief kill a tasklet, it will remove the tasklet from the tasklet list
 * @param tasklet tasklet
 * @return none
 */
void tasklet_kill(struct tasklet *tasklet)
{
    if(tasklet->state == 0) {
        list_del_node(&tasklet->entry);
    }
}

